package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.dto.*;
import com.zhentao.service.AmapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 地图控制器
 */
@RestController
@RequestMapping("/api/map")
@Slf4j
@CrossOrigin
public class MapController {
    
    @Autowired
    private AmapService amapService;
    
    /**
     * 地理编码 - 地址转坐标
     */
    @PostMapping("/geocode")
    public Result<GeocodeResponse> geocode(@RequestBody GeocodeRequest request) {
        try {
            log.info("地理编码请求: {}", request);
            GeocodeResponse response = amapService.geocode(request);
            
            if ("1".equals(response.getStatus())) {
                return Result.success("地理编码成功", response);
            } else {
                return Result.error("地理编码失败: " + response.getInfo());
            }
            
        } catch (Exception e) {
            log.error("地理编码异常", e);
            return Result.error("地理编码异常: " + e.getMessage());
        }
    }
    
    /**
     * 逆地理编码 - 坐标转地址
     */
    @PostMapping("/regeocode")
    public Result<RegeocodeResponse> regeocode(@RequestBody RegeocodeRequest request) {
        try {
            log.info("逆地理编码请求: {}", request);
            RegeocodeResponse response = amapService.regeocode(request);
            
            if ("1".equals(response.getStatus())) {
                return Result.success("逆地理编码成功", response);
            } else {
                return Result.error("逆地理编码失败: " + response.getInfo());
            }
            
        } catch (Exception e) {
            log.error("逆地理编码异常", e);
            return Result.error("逆地理编码异常: " + e.getMessage());
        }
    }
    
    /**
     * 搜索周边POI
     */
    @PostMapping("/search-poi")
    public Result<AmapPoiSearchResponse> searchNearbyPoi(@RequestBody AmapPoiSearchRequest request) {
        try {
            log.info("POI搜索请求: {}", request);
            AmapPoiSearchResponse response = amapService.searchNearbyPoi(request);
            
            if ("1".equals(response.getStatus())) {
                return Result.success("POI搜索成功", response);
            } else {
                return Result.error("POI搜索失败: " + response.getInfo());
            }
            
        } catch (Exception e) {
            log.error("POI搜索异常", e);
            return Result.error("POI搜索异常: " + e.getMessage());
        }
    }
    
    /**
     * 计算两点间距离
     */
    @GetMapping("/distance")
    public Result<Integer> calculateDistance(
            @RequestParam String origin,
            @RequestParam String destination) {
        try {
            log.info("距离计算请求: origin={}, destination={}", origin, destination);
            Integer distance = amapService.calculateDistance(origin, destination);
            
            if (distance >= 0) {
                return Result.success("距离计算成功", distance);
            } else {
                return Result.error("距离计算失败");
            }
            
        } catch (Exception e) {
            log.error("距离计算异常", e);
            return Result.error("距离计算异常: " + e.getMessage());
        }
    }
    
    /**
     * 获取位置信息
     */
    @GetMapping("/location-info")
    public Result<RegeocodeResponse> getLocationInfo(
            @RequestParam Double latitude,
            @RequestParam Double longitude) {
        try {
            log.info("获取位置信息请求: lat={}, lng={}", latitude, longitude);
            RegeocodeResponse response = amapService.getLocationInfo(latitude, longitude);
            
            if ("1".equals(response.getStatus())) {
                return Result.success("获取位置信息成功", response);
            } else {
                return Result.error("获取位置信息失败: " + response.getInfo());
            }
            
        } catch (Exception e) {
            log.error("获取位置信息异常", e);
            return Result.error("获取位置信息异常: " + e.getMessage());
        }
    }
    
    /**
     * 搜索周边快修店
     */
    @GetMapping("/repair-shops/nearby")
    public Result<AmapPoiSearchResponse> searchNearbyRepairShops(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "5000") Integer radius) {
        try {
            log.info("搜索周边快修店: lat={}, lng={}, radius={}", latitude, longitude, radius);
            
            AmapPoiSearchRequest request = new AmapPoiSearchRequest();
            request.setLocation(longitude + "," + latitude);
            request.setKeywords("汽车维修");
            request.setTypes("汽车服务|汽车维修");
            request.setRadius(radius);
            request.setSortrule("distance");
            request.setOffset(20);
            request.setPage(1);
            
            AmapPoiSearchResponse response = amapService.searchNearbyPoi(request);
            
            if ("1".equals(response.getStatus())) {
                return Result.success("搜索周边快修店成功", response);
            } else {
                return Result.error("搜索周边快修店失败: " + response.getInfo());
            }
            
        } catch (Exception e) {
            log.error("搜索周边快修店异常", e);
            return Result.error("搜索周边快修店异常: " + e.getMessage());
        }
    }
}
